propsage.namespace("modules.admin.users");

modules.admin.users.commission = (function($, ui) {
	var prevOverrideId;

	function initComponents()
	{
		$('#deleteDialog').dialog({
			'width': 420,
			'height': 220,
			'autoOpen': false,
			'resizable': false,
			'modal': true,
			'buttons': {
				'Cancel': onDialogCancel,
				'OK': {
					'text': 'Proceed',
					'class': 'blue',
					'click': onDeleteOK
				}
			},
			'open': onDeleteOpen
		});
	}

	function initEventHandlers()
	{
		$('#User_upline_user_id').change(onOverrideChange);
	}

	function onOverrideChange(event)
	{
		$('#tiers table').remove();
		$('#tiers').hide();

		var selectedOverride = $('#User_upline_user_id').val();

		ui.doAjax('POST', baseUrl + '/admin/users/ajaxGetOverridings', {
			id: selectedOverride,
			user_id: userId
		}, onOverrideChangeSuccess, onOverrideChangeFailure);
	}
	
	function onActionDeleteClick()
	{
		var selectedId = $(this).closest('tr').find('input:hidden').val();
		var selectedClass = $(this).closest('div[model-class]').attr('model-class');
		
		$('#model-id').val(selectedId);
		$('#model-class').val(selectedClass);
		
		$('#deleteDialog').dialog('open');
	}
	
	function onDeleteOpen()
	{
		
	}
	
	function onDialogCancel()
	{
		$(this).dialog('close');
	}
	
	function onDeleteOK()
	{
		var selectedId = $('#model-id').val();
		var selectedClass = $('#model-class').val();
		var reason = $('#delete-reason').val();
		
		
		if(!reason){
			window.alert('Please enter a reason for deleting.');
			return false;
		}
		
		switch(selectedClass){
			case 'Promotion':
				var action = 'ajaxDeletePromotion';
				break;
			case 'ScoreAdjustment':
				var action = 'ajaxDeleteScoreAdjustement';
				break;
		}
		
		ui.doAjax('POST', baseUrl + '/admin/users/' + action, {
			id : selectedId,
			delete_reason : reason
		}, onDeleteSuccess);
	}
	
	function onDeleteSuccess(response)
	{
		$('#deleteDialog').dialog('close');
		
		if (response.result == 'success'){
			$.fn.yiiGridView.update(response.model + "-grid"); 
			$('#delete-reason').val('');
			$('#model-id').val('');
			$('#model-class').val('');
			ui.growl('Record deleted');
		} 
	}

	function onOverrideChangeSuccess(response) {
		if (response.result == 'success') {

			var $table = $('<table></table>');
			$table.addClass('tierTable');

			var data = response.data;

			if (data.length > 0) {

				var $row = $('<tr></tr>');
				for (var key in data) {
					key = Number(key) + 1;
					var string = '<td>Tier ' + key + '</td>';
					$row.append(string);
				}
				$row.appendTo($table);
				$row = $('<tr></tr>');
				for (var key in data) {
					var string = '<td>' + data[key].name + '</td>';
					$row.append(string);
				}
				$row.appendTo($table);
				$table.appendTo('#tiers');
				prevOverrideId = $("#User_upline_user_id option:selected").val();
				$('#tiers').show();
			} else {
				$('#tiers').hide();
			}
		} else {
			$('#User_upline_user_id').val(prevOverrideId);
		}
	}

	function onOverrideChangeFailure() {
		$('#User_upline_user_id').val(prevOverrideId);
	}


	$(document).ready(function() {
		initComponents();
		initEventHandlers();
		onOverrideChange();

		prevOverrideId = $("#User_upline_user_id option:selected").val();
	});

	return {
		'onActionDeleteClick': onActionDeleteClick
	}

}(jQuery, propsage.ui));